package welsir.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Arg;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import welsir.entity.dos.Article;
import welsir.entity.dos.ArticleClass;
import welsir.entity.dos.User;
import welsir.entity.vo.ArticlePageVO;
import welsir.entity.vo.ArticleVO;
import welsir.mapper.ArticleClassMapper;
import welsir.mapper.ArticleMapper;
import welsir.service.IArticleClassService;
import welsir.service.IArticleCommentService;
import welsir.service.IArticleService;
import welsir.service.IUserService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Genius
 * @date 2023/03/07 21:36
 **/
@Service
public class IArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {

    @Resource
    private ArticleMapper articleMapper;

    @Resource
    private ArticleClassMapper articleClassMapper;

    @Resource
    private IArticleCommentService articleCommentService;

    @Autowired
    private IUserService userService;

    @Override
    public ArticlePageVO VoList(Integer page, Integer limit, Integer click) {
        List<ArticleVO> result = new ArrayList<>();

        ArticleVO articleVO = null;

        Page<Article> articlePage = new Page<>(page,limit);

        QueryWrapper<Article> wrapper= new QueryWrapper<Article>();
        if (click != 0) {
            wrapper.eq("sort_class", click);
        }
        wrapper.orderByDesc("id");

        Page<Article> resultPage = this.articleMapper.selectPage(articlePage, wrapper);


        List<Article> articles = resultPage.getRecords();
        long total = resultPage.getTotal();

        for (Article article : articles) {
            articleVO = new ArticleVO();
            BeanUtils.copyProperties(article,articleVO);
            String author = article.getAuthor();
            User user = userService.queryUserByUserName(author);
            if(user != null){
                String avatar = user.getAvatar();
                articleVO.setAvatar(avatar);
            }
            Integer aid = article.getId();
            int articleComment = articleCommentService.GetCommentNum(aid);
            articleVO.setCommentNum(articleComment);

            String sortClass =article.getSortClass();
            ArticleClass articleClass = articleClassMapper.selectById(sortClass);
            String className = articleClass.getName();

            articleVO.setClassName(className);

            result.add(articleVO);
        }

        ArticlePageVO articlePageVO = new ArticlePageVO();
        articlePageVO.setData(result);
        articlePageVO.setTotal(resultPage.getTotal());
        articlePageVO.setPages(resultPage.getPages());
        return articlePageVO;
    }

    @Override
    public ArticlePageVO FindVoList(Integer page, Integer limit, String title) {
        List<ArticleVO> result = new ArrayList<>();
        ArticleVO articleVO = null;

        Page<Article> articlePage = new Page<>(page,limit);

        QueryWrapper<Article> wrapper= new QueryWrapper<Article>();
        wrapper.orderByDesc("id") .like("title",title);

        Page<Article> resultPage = this.articleMapper.selectPage(articlePage, wrapper);

        List<Article> articles = resultPage.getRecords();
        long total = resultPage.getTotal();

        for (Article article : articles) {
            articleVO = new ArticleVO();
            BeanUtils.copyProperties(article,articleVO);
            String author = article.getAuthor();
            User user = userService.queryUserByUserName(author);
            String avatar = user.getAvatar();
            articleVO.setAvatar(avatar);
            result.add(articleVO);
        }

        ArticlePageVO articlePageVO = new ArticlePageVO();
        articlePageVO.setTotal(total);
        articlePageVO.setData(result);
        return articlePageVO;
    }

    @Override
    public Boolean articlesBrowse(Integer id) {
        return update().setSql("hits = hits+1 where id = "+id).update();
    }

    @Override
    public Boolean articlesLoveBrowse(Integer id) {
        return update().setSql("love_num = love_num+1 where id = "+id).update();
    }


}
